using FlickrNet;
using System;
using System.ComponentModel;
using WindowsLive.Writer.Api;

namespace TrailImage
{
    public class Profile
	{
		IProperties _settings;
		static Profile _current = null;

		public Profile(IProperties pluginSettings) { _settings = pluginSettings; }

		public static Profile Current
		{
			get { return _current; }
			set { _current = value; }
		}

		/// <summary>
		/// The image size to insert into the blog posting
		/// </summary>
		public ImageSize ImageSize
		{
			get { return (_settings.GetInt("ImageSize", (int)ImageSize.Large1024)).ToEnum<ImageSize>(); }
			set { _settings.SetInt("ImageSize", (int)value); }
		}

		/// <summary>
		/// Wrap tag for haiku
		/// </summary>
		public string HaikuWrapTag
		{
			get { return _settings.GetString("HaikuWrapTag", "<p class='haiku'/>"); }
			set { _settings.SetString("HaikuWrapTag", value); }
		}

		/// <summary>
		/// The HTML pattern to use when inserting images into the blog posting
		/// </summary>
		/// <remarks>
		/// The pattern should include {url} and {title} for replacement
		/// </remarks>
		public string ImagePattern
		{
			get { return _settings.GetString("HtmlPattern", string.Empty); }
			set { _settings.SetString("HtmlPattern", value); }
		}

		public string LastUsedSet
		{
			get { return _settings.GetString("LastSetID", string.Empty); }
			set { _settings.SetString("LastSetID", value); }
		}

        public string UserName
		{
            get
			{
				var name = _settings.GetString("FlickrUserName", string.Empty);
				if (string.IsNullOrEmpty(name))
				{
					// lookup user details if we don't have them
					var login = FlickrPlugin.Flickr.TestLogin();
					if (login != null)
					{
						this.UserName = name = login.UserName;
						this.UserID = login.UserId;
					}
				}
				return name;
			}
            set { _settings.SetString("FlickrUserName", value); }
        }

        public string UserID
		{
            get
			{
				var id = _settings.GetString("FlickrUserId", string.Empty);
				if (string.IsNullOrEmpty(id))
				{
					// lookup user details if we don't have them
					var login = FlickrPlugin.Flickr.TestLogin();
					if (login != null)
					{
						this.UserName = login.UserName;
						this.UserID = id = login.UserId;
					}
				}
				return id;
			}
            set { _settings.SetString("FlickrUserId", value); }
        }

		/// <summary>
		/// Authentication token supplied by Flickr
		/// </summary>
		/*
        public FlickrNet.OAuthAccessToken Token
		{
            get
			{
				//return _settings.GetString("FlickrToken", string.Empty);
				var token = new FlickrNet.OAuthAccessToken();
				token.Token = _settings.GetString("FlickrTokenSecret", string.Empty);
				token.TokenSecret = _settings.GetString("FlickrToken", string.Empty);					
				return token;
			}
            set
			{
				_settings.SetString("FlickrToken", value.Token);
				_settings.SetString("FlicrkTokenSecret", value.TokenSecret);
			}
        }
		*/

		public FlickrNet.OAuthAccessToken AccessToken
		{
			set
			{
				this.Token = value.Token;
				this.TokenSecret = value.TokenSecret;
				this.UserID = value.UserId;
				this.UserName = value.Username;
			}
		}

		/// <summary>
		/// Authentication token secret supplied by Flickr
		/// </summary>
		public string TokenSecret
		{
			get { return _settings.GetString("FlickrTokenSecret", string.Empty); }
			set { _settings.SetString("FlickrTokenSecret", value); }
		}

		/// <summary>
		/// Authentication token supplied by Flickr
		/// </summary>
		public string Token
		{
			get { return _settings.GetString("FlickrToken", string.Empty); }
			set { _settings.SetString("FlickrToken", value); }
		}

		/// <summary>
		/// Whether or not to show thumbnails with the photo information retrieved
		/// from Flickr
		/// </summary>
		public bool ShowThumbnails
		{
			get { return _settings.GetBoolean("ShowThumbnails", false); }
			set { _settings.SetBoolean("ShowThumbnails", value); }
		}

		/// <summary>
		/// When validating images, should the anchor "alt" attribute be
		/// updated with the current Flickr title
		/// </summary>
		public bool ReplaceAltWithTitle
		{
			get { return _settings.GetBoolean("ReplaceAltWithTitle", false); }
			set { _settings.SetBoolean("ReplaceAltWithTitle", value); }
		}

		/// <summary>
		/// Indicates if token is present not if it is still authorized at Flickr
		/// </summary>
		public bool IsEmptyToken { get { return string.IsNullOrEmpty(this.Token); } }
    }
}